package com.store.app.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.TaskOtherDetialsListAdapter;
import com.store.app.bean.SubTaskDetailsBean;
import com.store.app.bean.TaskDetailsListBean;
import com.store.app.bean.TaskHeadBean;
import com.store.app.bean.TaskLogBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/7.
 * 待审核、待结算、已经完成的详情页面
 */
public class OtherTaskDetailsActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ListView lv;
    private TaskOtherDetialsListAdapter adapter;
    private List<TaskDetailsListBean> taskDetailsListBeanList = new ArrayList<>();
    private List<TaskLogBean> logBeanList = new ArrayList<>();
    private Dialog dialog;
    private NetRequest netRequest;

    private String processing_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_task_detials);
        ExitApplication.getInstance().addActivity(this);
        initView();
        if (getIntent() != null) {
            processing_id = getIntent().getStringExtra("processing_id");
            netRequest = new NetRequest(this);
            netRequest.requestSubTaskDetails(1, processing_id);
            netRequest.requestLogList(5, processing_id);
        }
    }

    private void initView() {
        netRequest = new NetRequest(this);
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("详情");
        lv = (ListView) findViewById(R.id.lv);

        adapter = new TaskOtherDetialsListAdapter(this, taskDetailsListBeanList, logBeanList, new TaskOtherDetialsListAdapter.OnClickOrder() {
            @Override
            public void changePhoto(TaskDetailsListBean bean, int index) {
            }
        });
        lv.setAdapter(adapter);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                jsonObject.getString("detail");
                Gson gson = new Gson();
                SubTaskDetailsBean subTaskDetailsBean = gson.fromJson(jsonObject.getString("detail").toString(), SubTaskDetailsBean.class);
                TaskHeadBean taskHeadBean = new TaskHeadBean();
                taskHeadBean.setTv_expiration_date(subTaskDetailsBean.getExpried_date());
                taskHeadBean.setTv_reward(subTaskDetailsBean.getAmount());
                taskHeadBean.setPayment_way_key(subTaskDetailsBean.getPayment_way_key());
                taskHeadBean.setTv_content(subTaskDetailsBean.getDesc1());
                taskHeadBean.setTv_content_title(subTaskDetailsBean.getTask_name());
                adapter.setTaskHeadBean(taskHeadBean);
                Map<String, String> map = Utils.getMapForJson(jsonObject.getString("doc_url"));
                JSONArray jsonArray = new JSONArray(subTaskDetailsBean.getJson_data());
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject temJsonObj = jsonArray.getJSONObject(i);
                    String path_id = temJsonObj.getString("path_id");
                    String title = temJsonObj.getString("title");
                    String url = map.get(path_id);
                    TaskDetailsListBean taskDetailsListBean = new TaskDetailsListBean();
                    taskDetailsListBean.setPicUrl(url);
                    taskDetailsListBean.setTitle(title);
                    taskDetailsListBeanList.add(taskDetailsListBean);
                }
                adapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //查询日志
        else if(requestId ==5){
            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray  = jsonObject.getJSONArray("list");
                for (int i = 0;i<jsonArray.length();i++){
                    JSONObject jsonObject1 =  jsonArray.getJSONObject(i);
                    TaskLogBean logBean = new TaskLogBean(jsonObject1.getString("event_desc"), jsonObject1.getString("tracked_date"));
                    logBeanList.add(logBean);
                }
                adapter.notifyDataSetChanged();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }



}
